<?php

require('config.php');

if(!isset($_SESSION['id_user']))
    redirectFront('connexion');

$page = new rootLayoutCategory();
$page->setBaliseTitle('Modification de votre mot de passe');

//-- menu Left show products--------------------------------//
$menu = new layoutMenu();
$menuCategorie = new layoutMenuCategorie();
$menu->pushMenu($menuCategorie);
$menuPage = new layoutMenuPage();
$menu->pushMenu($menuPage);
$page->setMenu($menu);
$user = modelUser::getUser($_SESSION['id_user']);

if(isset($_POST['submit'])) {
    try {
    	$language = modelUser::getUserLanguage($_SESSION['id_user'],$_POST['language_default']);
    	if($language->id_user_langauge){
    		modelUser::setLanguageDefault($language->id_user_langauge,$_SESSION['id_user']);
    	}else{
    		$id =  modelUser::linkLanguage($_SESSION['id_user'], $_POST['language_default'],modelUser::LANG_DEFAULT);
    		modelUser::setLanguageDefault($id,$_SESSION['id_user']);
    	}
    	
    	
        $msg = new Message('Your language default has been changed', Message::INFO);
        Persistence::register(PERSIST_MESSAGE, $msg);
        redirectFront('profil');
    }
    catch(Exception $e) {
        $msg = new Message($e->getMessage(), Message::AVERT);
        Persistence::register(PERSIST_MESSAGE, $msg);
    }
}

//-- corps de la page -----------//
$tpl_file='profil-user-language-default.tpl';
$profil_user_language_default = new flyLayout(REP_TPL . $tpl_file);
$profil_user_language_default->start();

$tabs = new layoutTabs();
$tabs->pushLink('Mon compte', '/profil.php');
$tabs->pushLink('Mes messages', '/profil-message.php');
$profil_user_language_default->includeLayout('tabs', $tabs);


	$listLanguage = modelLanguage::getList();
	if($listLanguage->size() > 0){
		foreach($listLanguage as $key => $language){
			$profil_user_language_default->setVariable('language', $language->name);
			$profil_user_language_default->setVariable('id_language', $language->id_language);
			$language->code==$user->code?$profil_user_language_default->setVariable('selected', 'selected'):$profil_user_language_default->setVariable('selected', '');
			$profil_user_language_default->parseList('list-language');
		}
	}
$profil_user_language_default->stop();

$page->setBody($profil_user_language_default);

$page->display();

?>